package model.aluno;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.banco.ConexaoFactory;
import model.pessoa.Aluno;

public class InstrutorDAO {
	
	private Connection connection;
	
	public InstrutorDAO(){
		connection = new ConexaoFactory().getConnection();
	}
	
	public void salvarAluno(Aluno aluno) {
		String sql = "INSERT INTO ACADEMIA.ALUNO (NOME, DATA_NASCIMENTO, RG, CPF, DATA_NASCIMENTO, MATRICULA, PESO, ALTURA, ID_ENDERECO, TELEFONE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		//String sqlEndereco = "INSERT INTO ENDERECO "
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(2, aluno.getNome());
			statement.setString(5, aluno.getRg());
			statement.setString(4, aluno.getCpf());
			statement.setString(6, aluno.getDataNascimento());
			statement.setString(1, aluno.getMatricula());
			statement.setDouble(9, aluno.getPeso());
			statement.setDouble(9, aluno.getAltura());
			statement.setString(3, aluno.getTelefone());
			statement.execute();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	public List<Aluno> pesquisarAluno(String matricula, String nome) {
		
		String sql = "select id, matricula, nome, rg, cpf from aluno where codigo like '%" + matricula + "%' and descricao like '%" + nome + "%'";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			
			ResultSet rs = statement.executeQuery();
			
			List<Aluno> alunos = new ArrayList<Aluno>();
			
			while(rs.next()){
				Aluno aluno = new Aluno();
				
				aluno.setId(rs.getLong("id"));
				aluno.setMatricula(rs.getString("matricula"));
				aluno.setNome(rs.getString("nome"));
				aluno.setRg(rs.getString("rg"));
				aluno.setCpf(rs.getString("cpf"));
				
			}
			
			rs.close();
			statement.close();
			return alunos;
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return null;
	}
	
	public static void main(String[] args) {
		InstrutorDAO dao = new InstrutorDAO();
		
		Aluno aluno = new Aluno();
		
		aluno.setMatricula("111");
		aluno.setNome("Carlos");
		aluno.setRg("11111");
		aluno.setCpf("2222");
		aluno.setTelefone("8589876754");
		aluno.setDataNascimento("21/02/1990");
		aluno.setPeso(80);
		aluno.setAltura(1.70);
		
	}

}
